package com.example.controller;

import com.example.pojo.Result;
import com.example.utils.AliOSSUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(tags = "阿里云上传图片模块")
@Slf4j
@RestController
@RequestMapping("/api")
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @ApiOperation("上传图片，返回url")
    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile file) throws IOException {
        log.info("文件上传，文件名:{}",file.getOriginalFilename());
        //调用阿里云OSS工具类进行文件上传
        String url = aliOSSUtils.upload(file);
        log.info("文件上传完成，文件访问的url:{}",url);
        return Result.success(url);

    }
}
